package com.wyp.didishige.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.Toast;

import com.wyp.didishige.activity.ChatActivity;
import com.wyp.didishige.model.Rating;
import com.wyp.didishige.model.Request;
import com.wyp.didishige.model.User;

import java.util.List;

import cn.bmob.im.BmobChatManager;
import cn.bmob.im.bean.BmobMsg;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 封装一些经常用到的操作
 * Created by horry on 2015/4/28.
 */
public class OperationUtils {
    public static void grabRequest(final Context context, final Button grab, Request request, final User shiGe) {
        BmobQuery<Request> query = new BmobQuery<Request>();
        query.getObject(context, request.getObjectId(), new GetListener<Request>() {
            @Override
            public void onSuccess(Request request) {
                if (request != null) {
                    if (request.isAccept()) {
                        grab.setEnabled(false);
                        grab.setText("被抢");
                    }

                    refreshRequest(context, request, shiGe, grab);
                }
            }

            @Override
            public void onFailure(int i, String s) {

            }
        });
    }

    public static void refreshRequest(final Context context, final Request request, final User shiGe, final Button grab) {
        request.setAccept(true);
        request.setShiGeObject(shiGe);
        request.setShiGe(shiGe.getUsername());
        request.setShiGeNick(shiGe.getNick());
        request.update(context, new UpdateListener() {
            @Override
            public void onSuccess() {
                grab.setText("被抢");
                grab.setEnabled(false);
                setRating(context, shiGe);
                BmobMsg msg = BmobMsg.createTextSendMsg(context,
                        request.getUser().getObjectId(), "我抢到你的单子咯");

                gotoChat(context, msg, request);
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(context, "抢单失败", Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 接单成功后更新或新建评价数据
     */
    public static void setRating(final Context context, final User shiGe) {
        BmobQuery<Rating> query = new BmobQuery<>();
        query.addWhereEqualTo("username", shiGe);
        query.findObjects(context, new FindListener<Rating>() {
            @Override
            public void onSuccess(List<Rating> ratings) {
                Rating rating;
                if (ratings != null && ratings.size() != 0) {
                    rating = ratings.get(0);
                    rating.setServiceNum(rating.getServiceNum() + 1);
                    rating.update(context);
                } else {
                    rating = new Rating();
                    rating.setUsername(shiGe.getUsername());
                    rating.setServiceNum(1);
                    rating.save(context);
                }
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    public static void gotoChat(Context context, final BmobMsg bmobMsg, Request request) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("user", request.getUser());
        IntentUtil.start_activity((android.app.Activity) context, ChatActivity.class, bundle);
        Toast.makeText(context, "你已成功抢单，详细咨询一下吧", Toast.LENGTH_LONG).show();
        BmobMsg mMsg = BmobMsg.createTextSendMsg(context, bmobMsg.getBelongId(), "我抢到你的单子咯");
        BmobChatManager manager = BmobChatManager.getInstance(context);
        manager.sendTextMessage(request.getUser(), mMsg);
    }

    /**
     * @return void
     * @throws
     * @Title: startImageAction
     */
    public static void startImageAction(Activity activity, Uri uri, int outputX, int outputY,
                                        int requestCode, boolean isCrop) {
        Intent intent = null;
        if (isCrop) {
            intent = new Intent("com.android.camera.action.CROP");
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", outputX);
            intent.putExtra("outputY", outputY);
        } else {
            intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        }
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", isCrop);

        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra("return-data", true);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        activity.startActivityForResult(intent, requestCode);
    }
}
